//
//  DJVideoDraftVC.m
//  daojiVideo
//
//  Created by tikeios_002 on 2024/5/15.
//

#import "DJVideoDraftVC.h"
#import "DJVideoDraftModel.h"
#import "DJVideoDraftCell.h"
#import "DJUploadVC.h"
#import "QYMainViewController.h"
#import "QYSessionListViewController.h"
#import "QYDetailViewController.h"
#import "SYCollectionViewRoundFlowLayout.h"

@interface DJVideoDraftVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *draftCollection;
@property (weak, nonatomic) IBOutlet UIButton *allButton;
@property (weak, nonatomic) IBOutlet UIButton *delButton;

@property (strong,nonatomic)NSMutableArray * defulArr;
@property (strong,nonatomic)NSMutableArray <DJVideoDraftModel *>* listArr;
@property (assign,nonatomic)CGFloat itemWdith;
@property (strong,nonatomic) PublicView * nullView;
@property (strong,nonatomic)UIButton * rightButton;
@property (strong,nonatomic)NSMutableArray * delSeleteArr;
@end

@implementation DJVideoDraftVC
- (NSMutableArray *)showTextViewToKey1:(NSString *)keyString{
    NSMutableArray *showTextViewArrTo1 = [NSMutableArray new];
    for (int i = 0; [showTextViewArrTo1 count] < 5; i++) {
        int  RandomNumber = arc4random() % 30;
        if(![showTextViewArrTo1 containsObject:[NSNumber numberWithInt:RandomNumber]]){
            [showTextViewArrTo1 addObject:[NSNumber numberWithInt:RandomNumber]];
        }
    }
    return showTextViewArrTo1;
}
- (NSInteger)showTextViewToTime1:(NSString *)time{
    NSDateFormatter *showTextViewFaTo1 = [[NSDateFormatter alloc] init];
    [showTextViewFaTo1 setDateFormat:@"yyyy-MM-dd"];
    return [[showTextViewFaTo1 dateFromString:time] timeIntervalSince1970];
}
- (NSMutableArray *)delSeleteArr{
    if(_delSeleteArr == nil){
        _delSeleteArr = [NSMutableArray new];
    }
    return _delSeleteArr;
}
- (NSMutableArray *)listArr{
    if(_listArr == nil){
        _listArr = [NSMutableArray new];
    }
    return _listArr;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.view.backgroundColor = viewback_black;
    self.listArr = [NSMutableArray new];
    self.defulArr = [NSMutableArray new];
    if([[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@_%@",VIDOE_DRAFT_KEY,[QZUserInfoModel share].mid]] != nil){
        [self.defulArr addObjectsFromArray:[[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@_%@",VIDOE_DRAFT_KEY,[QZUserInfoModel share].mid]]];
    }
    for (NSDictionary * dic in self.defulArr) {
        DJVideoDraftModel * model = [[DJVideoDraftModel alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        [self.listArr addObject:model];
    }
    if(self.listArr.count == 0){
        self.nullView.hidden = NO;
    }
    
    UILabel * keyLabel = [[UILabel alloc] init];
    keyLabel.textColor = [UIColor grayColor];
    keyLabel.text = @"key";
    keyLabel.textAlignment = NSTextAlignmentCenter;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:keyLabel];
    
    UILabel * valueLabel = [[UILabel alloc] init];
    valueLabel.textColor = [UIColor grayColor];
    valueLabel.text = @"value";
    valueLabel.textAlignment = NSTextAlignmentCenter;
       UILabel * oldshowSSSSTip3 = [[UILabel alloc]init];
    oldshowSSSSTip3.clipsToBounds = YES;
    oldshowSSSSTip3.text = @"kass显示区";
    oldshowSSSSTip3.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip3.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip3.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip3.hidden = YES;
    [self.view addSubview:oldshowSSSSTip3];
    [self.view addSubview:valueLabel];
    
    UITextView * keyTextView1 = [[UITextView alloc] init];
    keyTextView1.backgroundColor = [UIColor whiteColor];
    keyTextView1.font = [UIFont systemFontOfSize:17];
    keyTextView1.layer.borderColor = [UIColor grayColor].CGColor;
    keyTextView1.layer.borderWidth = 1.0f;
    keyTextView1.layer.cornerRadius = 6.0f;
       UILabel * oldshowSSSSTip4 = [[UILabel alloc]init];
    oldshowSSSSTip4.clipsToBounds = YES;
    oldshowSSSSTip4.text = @"kass显示区";
    oldshowSSSSTip4.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip4.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip4.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip4.hidden = YES;
    [self.view addSubview:oldshowSSSSTip4];
    [self.view addSubview:keyTextView1];
    
    UITextView * valueTextView1 = [[UITextView alloc] init];
    valueTextView1.backgroundColor = [UIColor whiteColor];
    valueTextView1.font = [UIFont systemFontOfSize:17];
    valueTextView1.layer.borderColor = [UIColor grayColor].CGColor;
    valueTextView1.layer.borderWidth = 1.0f;
    valueTextView1.layer.cornerRadius = 6.0f;
    [self.view addSubview:valueTextView1];
    
    UITextView * keyTextView2 = [[UITextView alloc] init];
    keyTextView2.backgroundColor = [UIColor whiteColor];
    keyTextView2.font = [UIFont systemFontOfSize:17];
    keyTextView2.layer.borderColor = [UIColor grayColor].CGColor;
    keyTextView2.layer.borderWidth = 1.0f;
    keyTextView2.layer.cornerRadius = 6.0f;
       UILabel * oldshowSSSSTip5 = [[UILabel alloc]init];
    oldshowSSSSTip5.clipsToBounds = YES;
    oldshowSSSSTip5.text = @"kass显示区";
    oldshowSSSSTip5.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip5.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip5.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip5.hidden = YES;
    [self.view addSubview:oldshowSSSSTip5];
    [self.view addSubview:keyTextView2];
}
-(void)showTextViewNavViewTo1{
    NSMutableArray * showTextViewNavViewNameArrTo1 = [NSMutableArray new];
    UIView * showTextViewNavViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewNavViewViewTo1.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:showTextViewNavViewViewTo1];
    UILabel * showTextViewNavViewLabelTo1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    NSInteger showTextViewNavViewNumberTo1 = arc4random_uniform(20);
    NSLog(@"：%ld", (long)showTextViewNavViewNumberTo1);
    showTextViewNavViewLabelTo1.text = [NSString stringWithFormat:@"%ld",(long)showTextViewNavViewNumberTo1];
    [showTextViewNavViewLabelTo1 addSubview:showTextViewNavViewLabelTo1];
    
    UIImageView * showTextViewNavViewLabelTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [showTextViewNavViewViewTo1 addSubview:showTextViewNavViewLabelTo2];
    
}
- (void)showOldAddNavViewToMainOldView1{
    UIView * oldBgSS = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    oldBgSS.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:oldBgSS];
    
    UIButton * sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreen_W-75, kStatusBarHeight+10, 60, 46)];
    sendBtn.layer.cornerRadius = 3;
    sendBtn.backgroundColor = [UIColor whiteColor];
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [sendBtn setTitleColor:ColorHex(@"#333333") forState:UIControlStateNormal];
    sendBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [sendBtn addTarget:self action:@selector(sendTap) forControlEvents:UIControlEventTouchUpInside];
    [oldBgSS addSubview:sendBtn];
    
    UIView * sendBg = [[UIView alloc]initWithFrame:CGRectMake(15, kStatusBarHeight+66, KScreen_W-30, (KScreen_H-KSafeAreaBottomHeight-25-(kStatusBarHeight+66))/2)];
    sendBg.backgroundColor = [UIColor whiteColor];
    sendBg.layer.cornerRadius = 3;
    [oldBgSS addSubview:sendBg];
    
    UILabel * sendTip = [[UILabel alloc]initWithFrame:CGRectMake(0, (sendBg.frame.size.height-40)/2, sendBg.frame.size.width, 40)];
    sendTip.text = @"发送区";
    sendTip.textColor = ColorHex(@"#f0f0f0");
    sendTip.textAlignment = NSTextAlignmentCenter;
    sendTip.font = [UIFont systemFontOfSize:28];
    [sendBg addSubview:sendTip];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = NSLocalizedString(@"草稿箱", nil);
    
//    self.delManrItem = [[UIBarButtonItem alloc] initWithTitle:@"管理" style:UIBarButtonItemStylePlain target:self action:@selector(draftRemove)];
//    self.navigationItem.rightBarButtonItem = self.delManrItem;
    
    _rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
    [_rightButton setTitle:@"管理" forState: UIControlStateNormal];
    _rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [_rightButton addTarget:self action:@selector(draftRemove) forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_rightButton];
    
    self.itemWdith = (KScreen_W-2)/2;
    SYCollectionViewRoundFlowLayout *layout = [[SYCollectionViewRoundFlowLayout alloc]init];
    layout.sectionHeadersPinToVisibleBounds = YES;
    layout.collectionCellAlignmentType = SYCollectionViewFlowLayoutAlignmentTypeByLeft;
    self.draftCollection.collectionViewLayout = layout;
    self.draftCollection.alwaysBounceVertical = YES;
    [self.draftCollection registerNib:[UINib nibWithNibName:@"DJVideoDraftCell" bundle:nil] forCellWithReuseIdentifier:@"DJVideoDraftCell"];
    self.draftCollection.delegate = self;
    self.draftCollection.dataSource = self;
    
    
    
    [self.allButton setTitle:@" 全选" forState:UIControlStateNormal];
    self.allButton.backgroundColor = ColorHex(@"d7e6e8");
    [self.allButton setImage:KImageNamed(@"ico_draft_selete_nor") forState:UIControlStateNormal];
    [self.allButton setTitleColor:ThreeColor forState:UIControlStateNormal];
    [self.allButton setImage:KImageNamed(@"ico_draft_selete_sel") forState:UIControlStateSelected];
    self.allButton.layer.cornerRadius = 10;
    self.allButton.clipsToBounds = YES;
    self.allButton.hidden = YES;
    
    [self.delButton setTitle:@"删除" forState:UIControlStateNormal];
    self.delButton.backgroundColor = MainColor;
    self.delButton.layer.cornerRadius = 10;
    self.delButton.clipsToBounds = YES;
    self.delButton.hidden = YES;
}
- (PublicView *)nullView{
    if(_nullView == nil){
        _nullView = [[PublicView alloc]initWithFrame:CGRectMake(0, (KScreen_H-KNav_H-200)/2, KScreen_W, 200)];
        _nullView.publicLabel.text = @"暂无草稿";
        _nullView.hidden = YES;
        _nullView.userInteractionEnabled = NO;
           UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:_nullView];
    }
    return _nullView;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.listArr.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DJVideoDraftCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DJVideoDraftCell" forIndexPath:indexPath];
    [cell findsShoeCell:self.listArr[indexPath.row]];
    if(self.rightButton.selected){
        cell.delButton.hidden = NO;
        if([self.delSeleteArr containsObject:@(indexPath.row)]){
            cell.delButton.selected = YES;
        }else{
            cell.delButton.selected = NO;
        }
    }else{
        cell.delButton.hidden = YES;
    }
    KWeakSelf
    cell.videoDraftTapBlock = ^(NSInteger index) {
        if([weakSelf.delSeleteArr containsObject:@(indexPath.row)]){
            [weakSelf.delSeleteArr removeObject:@(indexPath.row)];
            weakSelf.allButton.selected = NO;
        }else{
            [weakSelf.delSeleteArr addObject:@(indexPath.row)];
            if(weakSelf.delSeleteArr.count == weakSelf.defulArr.count){
                weakSelf.allButton.selected = YES;
            }
        }
        if(weakSelf.delSeleteArr.count == 0){
            [weakSelf.delButton setTitle:[NSString stringWithFormat:@"删除"] forState:UIControlStateNormal];
        }else{
            [weakSelf.delButton setTitle:[NSString stringWithFormat:@"删除（%ld）",weakSelf.delSeleteArr.count] forState:UIControlStateNormal];
        }

        [weakSelf.draftCollection reloadData];
    };
    return cell;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 2.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 2.0;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
 layout:(UICollectionViewLayout *)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 0, 0, 0);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.itemWdith, self.itemWdith/3*4);
}
- (void)showTextViewSubViewToViewTo1:(UIView * )subView{

    UIView * showTextViewSubViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewSubViewTo1.clipsToBounds = YES;
    [subView addSubview:showTextViewSubViewTo1];
    UILabel * showTextViewSubViewLabelTo2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    showTextViewSubViewLabelTo2.text = @"56464645";
    [showTextViewSubViewTo1 addSubview:showTextViewSubViewLabelTo2];
    
    UIImageView * showTextViewSubViewTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [subView addSubview:showTextViewSubViewTo2];

}
- (void)showTextViewSubViewToViewTo2:(UIView * )subView{

    UIView * showTextViewSubViewTo2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewSubViewTo2.clipsToBounds = YES;
    [subView addSubview:showTextViewSubViewTo2];
    UILabel * showTextViewSubViewLabelTo3 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    showTextViewSubViewLabelTo3.text = @"ppppp88888";
    [showTextViewSubViewTo2 addSubview:showTextViewSubViewLabelTo3];
    
    UIImageView * showTextViewSubViewTo3 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [subView addSubview:showTextViewSubViewTo3];

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if(self.rightButton.selected){
        return;
    }
    DJUploadVC * upload = [[DJUploadVC alloc]init];
    upload.modalPresentationStyle = UIModalPresentationFullScreen;
    upload.draftModel = self.listArr[indexPath.row];
    [self presentViewController:upload animated:YES completion:nil];
    KWeakSelf
    upload.draftsBlock = ^{
        [weakSelf.defulArr removeObjectAtIndex:indexPath.row];
        [weakSelf.listArr removeObjectAtIndex:indexPath.row];
        [[NSUserDefaults standardUserDefaults] setObject:weakSelf.defulArr forKey:[NSString stringWithFormat:@"%@_%@",VIDOE_DRAFT_KEY,[QZUserInfoModel share].mid]];
        [weakSelf.draftCollection reloadData];
    };
}
- (void)onTapSessionListEntrance {
    QYSessionListViewController *vc = [QYSessionListViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)onTap1:(UIGestureRecognizer *)recognizer {
    QYDetailViewController *vc = [[QYDetailViewController alloc] init];
    vc.index = 1;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)onTap2:(UIGestureRecognizer *)recognizer {
    QYDetailViewController *vc = [[QYDetailViewController alloc] init];
    vc.index = 2;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)onTap3:(UIGestureRecognizer *)recognizer {
    QYDetailViewController *vc = [[QYDetailViewController alloc] init];
    vc.index = 3;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)onTap4:(UIGestureRecognizer *)recognizer {
    QYDetailViewController *vc = [[QYDetailViewController alloc] init];
    vc.index = 4;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)draftRemove{
    if(self.defulArr.count == 0){
        return;
    }
    
    if(self.rightButton.selected == NO){
        self.rightButton.selected = YES;
        [self.rightButton setTitle:@"取消" forState:UIControlStateNormal];
        self.delButton.hidden = NO;
        self.allButton.hidden = NO;
    }else{
        self.rightButton.selected = NO;
        [self.rightButton setTitle:@"管理" forState:UIControlStateNormal];
        [self.delSeleteArr removeAllObjects];

        self.delButton.hidden = YES;
        self.allButton.hidden = YES;
        self.allButton.selected = NO;
        [self.delButton setTitle:@"删除" forState:UIControlStateNormal];
    }
    [self.draftCollection reloadData];
}
- (IBAction)allTap:(id)sender {
    self.allButton.selected = !self.allButton.selected;
    if(self.allButton.selected){
        [self.delSeleteArr removeAllObjects];
        for (int i=0; i<self.defulArr.count; i++) {
            [self.delSeleteArr addObject:@(i)];
        }
        [self.draftCollection reloadData];
        [self.delButton setTitle:[NSString stringWithFormat:@"删除（%ld）",self.delSeleteArr.count] forState:UIControlStateNormal];
    }else{
        [self.delSeleteArr removeAllObjects];
        [self.draftCollection reloadData];
        [self.delButton setTitle:@"删除" forState:UIControlStateNormal];
    }
}
- (IBAction)delTap:(id)sender {
    if(self.delSeleteArr.count == 0){
        [YBMsgPop showPop:@"请选择"];
        return;
    }
    for (NSNumber * number in self.delSeleteArr) {
        [self.defulArr removeObjectAtIndex:[number intValue]];
        [self.listArr removeObjectAtIndex:[number intValue]];
    }
    [[NSUserDefaults standardUserDefaults] setObject:self.defulArr forKey:[NSString stringWithFormat:@"%@_%@",VIDOE_DRAFT_KEY,[QZUserInfoModel share].mid]];
    [self.delSeleteArr removeAllObjects];
    [self.draftCollection reloadData];
    if(self.defulArr.count == 0){
        [self draftRemove];
        self.rightButton.hidden = YES;
        self.nullView.hidden = NO;
    }
    
}

@end
